package com.gee.util;

import java.io.PrintStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class FontUtil {
	public int CHARXSIZE;
	public int CHARYSIZE;
	public int CHARXSPACING;
	public int CHARYSPACING;
	private int SCREENWIDTH;
	private int SCREENHEIGHT;
	private static String fontName = "/res/other/font_range.png";
	// private static String fontName_Mini = "/res/other/font_range_small.png";
	private Image font;
	private boolean init;
	private int lineWidth;

	public FontUtil(int i, int j) {
		CHARXSIZE = 13;
		CHARYSIZE = 12;
		CHARXSPACING = 10;
		CHARYSPACING = 16;
		init = false;
		lineWidth = 100;
		SCREENWIDTH = i;
		SCREENHEIGHT = j;
		loadFontImage(fontName);
	}

	public void loadFontImage(String s1) {
		try {
			font = Image.createImage(s1);
			System.gc();
			init = true;
		} catch (Exception exception) {
			System.out.println("ERROR: chargement des Images");
			exception.printStackTrace();
		}
	}

	private int length(String s1) {
		return CHARXSPACING * s1.length();
	}

	public void print(Graphics g, int i, int j, String s1) {
		if (g == null)
			return;
		if (!init)
			return;
		int l = 0;
		int j2 = s1.length();
		int i1 = i;
		int j1 = j;
		for (int i2 = 0; i2 < j2; i2++) {
			char c = s1.charAt(i2);
			int k = 0;
			if (c >= 'a' && c <= 'z')
				k = c - 97;
			if (c >= 'A' && c <= 'Z')
				k = c - 65;
			if (c >= '0' && c <= '9')
				k = (c - 48) + 26;
			int k1 = (k % 12) * CHARXSIZE;
			int l1 = (k / 12) * CHARYSIZE;
			if (c != ' ' && j1 >= -CHARYSIZE && j1 <= SCREENHEIGHT) {
				g.setClip(i1, j1, CHARXSIZE, CHARYSIZE);
				g.drawImage(font, i1 - k1, j1 - l1, 0);
			}
			i1 += CHARXSPACING;
			if (++l == lineWidth) {
				i1 = i;
				j1 += CHARYSPACING;
				l = 0;
			}
		}

	}

	public void printMiddle(Graphics g, int i, int j, String s1) {
		int k = (SCREENWIDTH - s1.length() * CHARXSPACING) / 2;
		print(g, k + i, j, s1);
	}

	public void print(Graphics g, String as[], int i, int j) {
		int k = as.length;
		int l = (SCREENHEIGHT - k * CHARYSPACING >> 1) + j;
		for (int i1 = 0; i1 < k; i1++) {
			printMiddle(g, i, l, as[i1]);
			l += CHARYSPACING;
		}

	}

	public void print(Graphics g, String as[]) {
		print(g, as, 0, 0);
	}

	public void print(Graphics g, int i, int j, String s1, int k) {
		int l = 0;
		int i1 = 0;
		if ((k & 0x8) != 0)
			l = SCREENWIDTH - length(s1);
		if ((k & 0x20) != 0)
			i1 = SCREENHEIGHT - CHARYSIZE;
		print(g, l + i, i1 + j, s1);
	}

}
